package org.javaforever.cookieshop.dao;

import org.javaforever.cookieshop.domain.CustOrderBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface CustOrderDao extends JpaRepository<CustOrderBean, Long>{
	@Query("from CustOrderBean c")
	public List<CustOrderBean> listAllCustOrders() throws Exception;

	@Modifying
	@Query("delete from CustOrderBean c where c.id = :id")
	@Transactional
	public void deleteCustOrder(Long id) throws Exception;

	@Modifying
	@Query("update CustOrderBean c set c.active = false where c.id = :id")
	@Transactional
	public void softDeleteCustOrder(Long id) throws Exception;

	@Query("from CustOrderBean c where c.id = :id")
	public CustOrderBean findCustOrderById(Long id) throws Exception;

	@Query("from CustOrderBean c where c.name = :name")
	public CustOrderBean findCustOrderByName(String name) throws Exception;

	@Query("from CustOrderBean c where c.active = true")
	public List<CustOrderBean> listActiveCustOrders() throws Exception;

	@Modifying
	@Query("update CustOrderBean c set c.active = not c.active where c.id = :id")
	@Transactional
	public void toggleCustOrder(Long id) throws Exception;

	@Modifying
	@Query("update CustOrderBean c set c.active = true where c.id = :id")
	@Transactional
	public void activateCustOrder(Long id) throws Exception;

	@Query("from CustOrderBean c where c.id = :id")
	public CustOrderBean findCustOrderFullInfoById(Long id) throws Exception;

	@Query("from CustOrderBean c where c.name like CONCAT(:shortName, '%')")
	public List<CustOrderBean> searchCustOrderIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from CustOrderBean c where c.active = true")
	public Integer countActiveCustOrderRecords() throws Exception;

	@Modifying
	@Query(value="insert into cs_cust_order_order_item ( cust_order_id,order_item_id) values (:custOrderId,:orderItemId)", nativeQuery=true)
	@Transactional
	public Integer assignOrderItemToCustOrder(Long custOrderId,Long orderItemId) throws Exception;

	@Modifying
	@Query(value="delete from cs_cust_order_order_item  where cust_order_id = :custOrderId and order_item_id = :orderItemId", nativeQuery=true)
	@Transactional
	public Integer revokeOrderItemFromCustOrder(Long custOrderId,Long orderItemId) throws Exception;

}
